package com.example.xcsubsystem.controller;

import com.example.xcsubsystem.service.LocalService;
import com.example.xcsubsystem.service.RemoteService;
import com.example.xcsubsystem.vo.ResBody;
import com.example.xcsubsystem.vo.ServiceStatusVO;
import com.example.xcsubsystem.vo.UserServiceVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 2024/9/28
 *
 * @author fengyu
 */
@Tag(name="提供给统一平台的接口", description= "接口管理器")
@RestController
@RequestMapping("/remote")
public class RemoteController {
    private static final Logger log = LoggerFactory.getLogger(RemoteController.class);

    @Resource
    private LocalService restService;

    @Resource
    private RemoteService remoteService;

    @Value("${remote.appKey}")
    private String appKey;

    @Value("${remote.appSecret}")
    private String appSecret;


    /**
     * 各系统的单点登录地址，接收统一平台传递的token
     * @param token 单点登录生成的token，统一平台和系统各自保存
     */
    @Operation(summary = "单点登录接口", description = "本系统在同一平台端的入口地址")
    @GetMapping(path = "login")
    public void login (@Parameter(description = "统一平台传递的token") String token) {
        // todo 缓存token
        // todo 调用token验证以获取登录用户信息
        try {
            log.info("点击进入系统传递的token参数： {}",token);
            remoteService.getUserFromToken(appKey, appSecret, token);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    /**
     * 提供给统一平台使用的接口，统一平台方授权指定用户访问本系统时调用该接口通知本系统做相应处理
     * @param accountId 用户浙政钉ID
     * @param status 0不允许，1允许
     * @return
     */
    @Operation(summary = "授权指定用户访问本系统", description = "统一平台方授权指定用户访问本系统")
    @PostMapping("empower")
    public ResBody updateAll(@Parameter(description = "用户浙政钉ID") @RequestParam String accountId,
                             @Parameter(description = "是否允许访问系统") @RequestParam int status){
        log.info("检查参数，是否正确，并做对应处理");
        boolean r = restService.empower(accountId, status > 0 );
        return new ResBody().success();
    }

    /**
     * 提供给统一平台调用的接口，将统一平台上注册的服务跟本系统内部服务关联起来
     * @param id 统一平台方生成的id
     * @param oid 资源服务的id
     * @return
     */
    @Operation(summary = "统一平台上注册的服务")
    @PostMapping("regist")
    public ResBody registService(@Parameter(description = "统一平台传来的ID") String id,
                                 @Parameter(description = "本系统的服务ID") String oid) {
        log.info("统一平台上注册了服务，将两方的id对应关联起来");

        try {
            restService.registService(id, Long.parseLong(oid));
        } catch (Exception e) {
            return new ResBody().failure(e.getMessage());
        }
        return new ResBody().success();
    }

    @Operation(summary = "检查服务在线状态")
    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET}, path = "check")
    public ResBody<List<ServiceStatusVO>> checkService() {
        log.info("检查服务的在线状态");

        return new ResBody().success(restService.getServiceStatus());
    }

    @Operation(summary = "用户申请服务")
    @GetMapping("apply")
    public void apply (@Parameter(name = "accountId", description = "浙政钉ID") String accountId,
                       @Parameter(name = "oid", description = "资源服务的ID") String oid) {
        log.info("用户申请服务");
        boolean r = restService.applyService(accountId, Long.parseLong(oid));
        if (!r) return;

        // todo 调用统一平台提供的接口，反馈服务待审批状态
    }

    @Operation(summary = "用户申请的所有服务审批状态")
    @PostMapping("us")
    public ResBody<List<UserServiceVO>> checkUserService(@Parameter(name = "accountId", description = "浙政钉ID") String accountId){
        log.info("查询用户的所有服务申请状态");
        List<UserServiceVO> list = restService.getUserServiceStatus(accountId);
        return new ResBody().success(list);
    }
}
